package com.miage.itrading.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.miage.itrading.dao.CotationDAO;
import com.miage.itrading.entites.Cotation;
import com.miage.itrading.utils.Util;

public class HautDePage extends JInternalFrame {
	
	
	public static float X 		= 0.15f;
	public static float Y		= 0;
	public static float WIDTH 	= 0.7f;
	public static float HEIGHT 	= 0.1f;
	
	private static final long serialVersionUID = -5974763956234648447L;
	private FenetrePrincipale parent;

	public HautDePage(final FenetrePrincipale parent){
        super("Menu - iTrading",  
                true, //resizable
                true, //closable
                true, //maximizable
                true);//iconifiable

		this.parent = parent;
		
		
		setLocation((int) (parent.getScreenSize().width * X), 
					(int) (parent.getScreenSize().height * Y));
	
		setSize((int) (parent.getScreenSize().width * WIDTH), 
				(int) (parent.getScreenSize().height * HEIGHT));
		
		setResizable(true);

		add(menu(), BorderLayout.WEST);
		add(barreRecherche(), BorderLayout.EAST);
	}



	private JPanel barreRecherche() {
		JPanel container = new JPanel();

		final JTextField barreDeRecherche;

		JLabel titreBarreDeRecherche = new JLabel("Recherche : ");
		barreDeRecherche = new JTextField();
		
		barreDeRecherche.setColumns(25);

		// Détecte si la touche entrée et exécute le code

		barreDeRecherche.addKeyListener(new java.awt.event.KeyAdapter() {  
			public void keyPressed(java.awt.event.KeyEvent e) {
				if(e.getKeyChar() == '\n')
				{
					CotationDAO dao = new CotationDAO();
					Cotation cot = new Cotation();

					try {
						
						cot = dao.getCotationByMnemo(barreDeRecherche.getText());
						parent.afficheCours(cot);
					} catch (NullPointerException e1) {
						try {
							
							cot = dao.getCotationByName(barreDeRecherche.getText());
							parent.afficheCours(cot);
						} catch (NullPointerException e3) {
						JOptionPane.showMessageDialog(null,"impossible de trouver la cotation "+ barreDeRecherche.getText());
						}
						catch (SQLException e4) {
							System.err.println("exception sql");
						}
					}
					catch (SQLException e2) {
						System.err.println("exception sql");
					}
					
					
				
				}
			}

			public void keyReleased(java.awt.event.KeyEvent e) { }

			public void keyTyped(java.awt.event.KeyEvent e) { }
		});

		container.add(titreBarreDeRecherche);
		container.add(barreDeRecherche);

		return container;
	}

	private JPanel menuIndices() {
		JPanel container = new JPanel();
		Cotation cotation;

		final CotationDAO c = new CotationDAO();
		ArrayList<Integer> idsIndicateurs;
		try {
			idsIndicateurs = (ArrayList<Integer>) (c.getIdsIndices());

			for (int i=0 ; i<idsIndicateurs.size() ; ++i) {
				/**
				 * Configuration de l'indice centré dans un label coloré
				 *  avec un listener pour l'afficher lors du clic
				 */
				cotation = c.getCotationById(idsIndicateurs.get(i));

				final JLabel indice = new JLabel(cotation.getNom() + " (" + Util.variationCoursString(cotation) + ")");

				indice.setForeground(Util.styleVariationCours(cotation));
				indice.setHorizontalAlignment(AbstractButton.CENTER);
				indice.setVerticalAlignment(AbstractButton.CENTER);
				indice.setToolTipText("Afficher le cours de l'indice " + cotation.getNom());

				indice.addMouseListener(new MouseListener() {
					public void mouseReleased(MouseEvent arg0) { }
					public void mousePressed(MouseEvent arg0) { }
					public void mouseExited(MouseEvent arg0) { }
					public void mouseEntered(MouseEvent arg0) { }
					public void mouseClicked(MouseEvent arg0) {
						try {

							String nom = indice.getText().split(" ")[0];

							parent.afficheCours(c.getCotationByName(nom));

						} catch (SQLException e1) {
							e1.printStackTrace();
						}
					}
				});

				container.add(indice);
			}
		} catch (SQLException e2) {
			e2.printStackTrace();
		}

		return container;
	}

	private JPanel menuBouton() {
		JPanel container = new JPanel();

		JButton bouton = new JButton("Screening");
		bouton.setHorizontalAlignment(AbstractButton.CENTER);
		bouton.setVerticalAlignment(AbstractButton.CENTER);
		bouton.setToolTipText("Recherche de cours");

		bouton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				new Screening(parent);
			}
		});

		container.add(bouton);

		return container;
	}
	private JComboBox listeActions(){
		final JComboBox liste = new JComboBox();
		final CotationDAO dao = new CotationDAO();
		liste.addItem("");
		try {
			for (Cotation c : dao.getAllCotations()){
				liste.addItem(c.getNom());
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		liste.setSelectedItem("");
		liste.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.out.println(liste.getSelectedItem().toString());
				try {
					parent.afficheCours(dao.getCotationByName(liste.getSelectedItem().toString()));
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		});

		return liste;
	}
	private JPanel menu()
	{
		JPanel container = new JPanel();
		container.add(menuIndices());
		container.add(menuBouton());
		container.add(listeActions());
		return container;
	}

}
